iT邦幫忙

2021 iThome 鐵人賽

DAY 9
0
Mobile Development

重新瞭解Android硬體控制系列 第 5

110/09 - 把照片儲存在Android/data/packageName/files/

  • 分享至 

  • xImage
  •  

今天來實作使用ActivityResultContracts.TakePicture()開啟相機,拍照後把照片儲存在Android/data/packageName/files/Pictures/001.jpg,因為是存在packageName裡面,所以不用寫權限相關的程式碼,非常方便。

開一個新的Activity來玩,首先一樣是頁面的部份

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CropLensActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/aclMbCreatePackageNamePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="4dp"
            android:layout_marginEnd="8dp"
            android:padding="8dp"
            android:text="在應用程式資料夾建立照片"
            android:textAllCaps="false"
            app:autoSizeTextType="uniform"
            app:layout_constraintBottom_toTopOf="@+id/aclIvPackageNamePicture"
            app:layout_constraintDimensionRatio="5:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/aclIvPackageNamePicture"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="4dp"
            android:layout_marginTop="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="4:3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/aclMbCreatePackageNamePicture"
            tools:src="@tools:sample/backgrounds/scenic" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

再來是點擊拍照的程式碼

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_crop_lens)
    aclMbCreatePackageNamePicture.setOnClickListener {
        val packageNameFile =
            File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "001.jpg")
        val uri = getPictureUri(packageNameFile)
        
        takePictureResultLauncher.launch(uri)
    }
}

private fun getPictureUri(picturePath: File): Uri? {
    return FileProvider.getUriForFile(
        this,
        "$packageName${BaseConstants.DOT_FILEPROVIDER}",
        picturePath
    )
}

啟動器的部份,確定相片儲存成功後,把相片的uri拿來顯示

private val takePictureResultLauncher =
    registerForActivityResult(ActivityResultContracts.TakePicture()) { isTakePicture ->
        if (!isTakePicture) {
            Log.d("maho", "拍照建立檔案失敗")
            return@registerForActivityResult
        }
        val packageNameFile =
            File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "001.jpg")
        
        MediaScannerConnection.scanFile(
            this,
            arrayOf(packageNameFile.toString(), phoneFile.toString()),
            null
        ) { s, uri ->
            Log.d("maho", "s1: $s / uri1: $uri")
        }
        
        aclIvPackageNamePicture.setImageURI(getPictureUri(packageNameFile))
    }

這樣寫完跟以前比起來少了很多程式碼,舒服。


上一篇
110/08 - 檔案要存那裡?
下一篇
110/10 - Intent.ACTION_MEDIA_SCANNER_SCAN_FILE棄用
系列文
重新瞭解Android硬體控制14
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言